package org.grow.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.basic.dao.MaterialMapper;
import org.grow.basic.entity.Material;
import org.grow.basic.entity.MaterialQueryCondition;
import org.grow.basic.entity.PagedResult;
import org.grow.basic.service.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xwg
 * @CreateDate: 2022/5/19
 */
//@CrossOrigin
//http://localhost:9091/basic/material/query
//http://localhost:9090/swimming/material/query
@RestController
//@RequestMapping("/basic/material")
@RequestMapping(value = {"/swimming/material","/basic/material"})
public class MaterialController {

    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private CodeGenerator codeGenerator;

    @PostMapping("/insert")
    public Material insert(@RequestBody Material material) {
        material.setMaterialCode(codeGenerator.nextCode("SP"));
        materialMapper.insert(material);
        return material;
    }

    @PostMapping("/query")
    public PagedResult<Material> query(@RequestBody MaterialQueryCondition
                                               materialQueryCondition) {
        Page<Material> materialPage =
                new Page<>(materialQueryCondition.getPageNum(), materialQueryCondition.getPageSize());
        IPage<Material> materialIPage =
                materialMapper.dynamicQuery(materialPage, materialQueryCondition);
        return new PagedResult<>(materialIPage.getTotal(), materialIPage.getRecords());
    }



}
